﻿using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Might.Infrastructure.Services;
using Might.UIControls.FileSystemBrowser;
using Might.UIControls.ObjectBrowser;
using Might.UIControls.PackageBrowser;
using Might.UIControls.PropertyEditor;
using Might.UIControls.StatusBar;
using Might.UIControls.DomBrowser;

namespace Might.UIControls {
    public class UIControlsModule : IModule {
        private readonly IRegionManager regionManager;
        private readonly IUnityContainer container;
        private readonly IEventAggregator eventAggregator;

        private FileSystemBrowserPresenter fsBrowserpresenter;
        private StatusBarPresenter statusBarPresenter;
        private ObjectBrowserPresenter objectBrowserPresenter;
        private DomBrowserPresenter domBrowserPresenter;

        public UIControlsModule(
            IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator) {
            this.regionManager = regionManager;
            this.container = container;
            this.eventAggregator = eventAggregator;
        }

        public void Initialize() {

            // 1. Init Might menu
            IRegion topMenuRegion = regionManager.Regions["TopMenuRegion"];

            ILoginView loginView = new LoginView();
            topMenuRegion.Add(loginView);


            // 2. Init Might left controls
            IRegion leftControlRegion = regionManager.Regions["LeftControlRegion"];
            leftControlRegion.Add(new LeftControlBar.LeftControlBar());


            // 3. Init Might left treeviews
            IRegion leftPanelRegion = regionManager.Regions["LeftPanelRegion"];

            //do not use cotainer for create Views! Both views has same presenter!
            IFileSystemBrowserView fsView = new FileSystemBrowserView();

            //FS Browser init
            container.Resolve<IMightLogger>().Log(this, "File system browser module init");
            fsBrowserpresenter = new FileSystemBrowserPresenter()
                                 {FSBrowserView = fsView, LoginView = loginView};
            leftPanelRegion.Add(fsView);

            // Package Browser Init
            container.Resolve<IMightLogger>().Log(this, "Package browser init");

            IPackageBrowserView packageBrowserView = container.Resolve<PackageBrowserView>();
            leftPanelRegion.Add(packageBrowserView);

            //Object browser init
            container.Resolve<IMightLogger>().Log(this, "Object browser init");

            objectBrowserPresenter = new ObjectBrowserPresenter( new ObjectBrowserView() );
            leftPanelRegion.Add(objectBrowserPresenter.View);

            //DOM browser init
            container.Resolve<IMightLogger>().Log( this , "DOM browser init" );

            domBrowserPresenter = new DomBrowserPresenter( new DomBrowserView() );
            leftPanelRegion.Add( domBrowserPresenter.View );


            // 4. Init Might Right Property view

            //property editor init
            container.Resolve<IMightLogger>().Log(this, "Property editor init");

            PropertyEditorView propertyEditorView = container.Resolve<PropertyEditorView>();
            IRegion propertyEditorRegion = regionManager.Regions["RightPanelRegion"];
            propertyEditorRegion.Add(propertyEditorView);


            // 5. Init status bar

            //status bar init
            StatusBarView statusBarView = container.Resolve<StatusBarView>();
            statusBarPresenter = new StatusBarPresenter() {View = statusBarView};
            IRegion statusBarRegion = regionManager.Regions["StatusBarRegion"];
            statusBarRegion.Add(statusBarView);

            // 6. Init Log - elsewhere
        }
    }
}
